from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as Ec
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import pytest,time,yaml,logging
def ass():
    with open('min.yaml','r',encoding='utf8')as f:
        return yaml.safe_load(f)
c=ass()
@pytest.fixture(scope='session')
def dr():
    dr=webdriver.Edge()
    logging.debug('用例执行前执行')
    dr.get('https://login.jiuxian.com/login.htm')
    dr.maximize_window()
    yield dr
    logging.debug('用例执行后执行')
    time.sleep(2)
    dr.quit()
# @pytest.mark.parametrize('data',c)
def test_001(dr):
    try:
        time.sleep(2)
        dr.find_element(By.XPATH,'//*[@id="userName"]').send_keys('15649920329')
        time.sleep(2)
        dr.find_element(By.ID,'password').send_keys('Ycy20040102')
        logging.debug('用例执行时执行')
        time.sleep(20)
        dr.find_element(By.XPATH,'//*[@id="save"]').click()
        time.sleep(2)
        dr.find_element(By.XPATH,'//a[@class="bindSub" and @id="bindSub"]').click()
        time.sleep(5)
        time.sleep(2)
        # if data['ass'] == '我的酒仙':
        #     dy1 = dr.find_element(By.XPATH, '/html/body/div[2]/div/div[2]/ul/li[1]/div[1]/a').text
        #     assert data['ass'] == dy1
        # else:
        #     dy2 = dr.find_element(By.XPATH, '/html/body/div/form/div/div/div/div/div[1]/div[1]/a[1]').text
        #     assert data['ass'] == dy2
        #     return
    except Exception as f:
        timee=time.strftime('%Y%m%d%H%S%D',time.localtime())
        dr.save_screenshot(r'D:\gaowu\pythonProject\zhokao22\zhokao'+timee+'.png')
        print(f)
def test_002(dr):
    test_001(dr)
    # 分别是强制等待，显示等待，隐示等待
    dy1=dr.find_element(By.XPATH,'/html/body/div[2]/div/div[2]/ul/li[1]/div[1]/a').text
    assert dy1=='我的酒仙'
    # 强制等待是强制让程序暂停
    time.sleep(2)
    # 显示等待根据代码等待二十秒每0.5秒查询一次
    a=WebDriverWait(dr,20,0.5).until(Ec.presence_of_element_located((By.ID,'wd')))
    time.sleep(2)
    a.send_keys('剑南春')
    logging.debug('用例执行时执行')
    a.send_keys(Keys.ENTER)
    # 隐示等待等待20秒
    dr.implicitly_wait(10)
    dr.find_element(By.XPATH,'/html/body/div[6]/div/div[2]/div[5]/ul/li[1]/div[2]/div[1]/a/img').click()
    time.sleep(2)
    dr.switch_to.window(dr.window_handles[-1])
    time.sleep(2)
    dr.find_element(By.NAME,'detail_jiarugouwuche').click()
    time.sleep(2)
    dr.find_element(By.XPATH,'//*[@id="u-buy-layId"]/div/div[3]/a[2]').click()
    time.sleep(2)
    dr.find_element(By.LINK_TEXT,'去结算').click()
    time.sleep(5)
    dr.find_element(By.XPATH,'//*[@id="submitOrder"]').click()
    time.sleep(2)
    dy2=dr.find_element(By.XPATH,'/html/body/div[3]/div/div/p[2]').text
    assert dy2=='收银台'
    a = dr.find_element(By.XPATH, '/html/body/div[3]/div/div/p[2]')
    print(a.size)
    print(a.tag_name)
    print(a.text)
    print(a.get_attribute('payCash'))
    print(a.is_displayed())
    print(a.is_selected())
def test_003(dr):
    test_001(dr)
    time.sleep(5)
    dr.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/div[1]/div[1]/ul/li[3]').click()
    time.sleep(2)
    dr.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/div[1]/div[2]/div[3]/ul/li[2]/div[1]/a/img').click()
    time.sleep(2)
    dr.switch_to.window(dr.window_handles[-1])
    time.sleep(2)
    logging.debug('用例执行时执行')
    dr.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[6]/div[6]/div/a').click()
    time.sleep(2)
    dr.find_element(By.XPATH,'/html/body/div[7]/div/div[3]/a[2]').click()
    time.sleep(2)
    dr.find_element(By.XPATH,'/html/body/div[3]/div[1]/div[3]/div[3]/a[1]').click()
    time.sleep(2)
    dr.find_element(By.XPATH,'/html/body/div[3]/div/div[7]/div/div[2]/div[3]/a[2]').click()
    time.sleep(10)
